/**
 * 
 */
package com.bitBusy.manageyourriches.utilityClasses.setters;

import java.util.LinkedList;
import java.util.List;

import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

/**
 * Class that listens to and informs of choice
 * @author SumaHarsha
 *
 */


public class InformChoiceSpinnerSubject implements SpinnerSubject{

	/**Listeners for item selected*/
	private List<SpinnerObserver> myListeners;
	
	/** spinner to set for*/
	private Spinner mySpinner;
	
	public InformChoiceSpinnerSubject(Spinner the_spinner)
	{
		mySpinner = the_spinner;
	}
	

	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemSelected(AdapterView<?> the_adapter, View the_view, int the_position,
			long the_id) {
		if (myListeners != null)
		{
			for(SpinnerObserver listener: myListeners)
			{
				 listener.notifyOfSelection(the_adapter.getItemAtPosition(the_position).toString(), mySpinner);
			}
		}
		
	}

	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.AdapterView)
	 */
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}


	/**Method to add listener*
	 * @param the listener to be added
	 */
	public void addListener(SpinnerObserver the_observer){
		if (the_observer != null)
		{
			if (myListeners == null)
			{
				myListeners = new LinkedList<SpinnerObserver>();
			}
			myListeners.add(the_observer);
		}
	}
	
	/** Method to remove listener
	 * @param the listener to be removed
	 */
	public void removeListener(SpinnerObserver the_observer)
	{
		if (the_observer != null && myListeners != null)
			{
				myListeners.remove(the_observer);
			}		
	}

}
